package com.xs.ahoi.hcxs.ui.dialog;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnItemClick;
import com.xs.ahoi.hcxs.R;
import com.xs.ahoi.hcxs.dao.CollectPathDao;
import com.xs.ahoi.hcxs.utils.MapUtil;
import com.xs.ahoi.hcxs.utils.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 收藏目录弹窗页面
 */
public class DialogCollectPathList extends BaseDialog {

    @BindView(R.id.dialogListView)
    public ListView dialogListView;

    private CollectPathDao collectPathDao;
    private List<Map<String, Object>> datas;
    private CollectPathListAdapter adapter;
    private CollectPathListListener collectPathListListener;

    public DialogCollectPathList(@NonNull Context context) {
        super(context, R.style.common_dialog);
    }

    @Override
    protected void onStart() {
        super.onStart();
        initData();
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_collect_path_list;
    }

    @Override
    protected int showPosition() {
        return Gravity.CENTER;
    }

    // 初始化
    @Override
    protected void initData() {
        collectPathDao = CollectPathDao.getInstance();

        datas = collectPathDao.listAll()
                .stream()
                .map(o -> MapUtil.build().put("path", o).over())
                .collect(Collectors.toList());

        // 绑定adapter
        adapter = new CollectPathListAdapter(getContext(), datas, R.layout.listview_item_collect_path, new String[]{"path"}, new int[]{R.id.tvPath});
        dialogListView.setAdapter(adapter);
    }

    @OnItemClick({R.id.dialogListView})
    public void onItemClick(int position) {
        Map<String, Object> objectMap = datas.get(position);
        String path = ObjectUtils.toString(objectMap.get("path"));
        collectPathListListener.moveToPath(path);
        dismiss();
    }

    public void setCollectPathListListener(CollectPathListListener collectPathListListener) {
        this.collectPathListListener = collectPathListListener;
    }


    public interface CollectPathListListener {
        void moveToPath(String path);

        void refresh();
    }


    //**********************************************************
    //********************get set 方法**************************
    //**********************************************************

    // 自定义SimpleAdapter
    private class CollectPathListAdapter extends SimpleAdapter {

        private CollectPathListAdapter(Context context, List<? extends Map<String, Object>> datas, int resource, String[] from, int[] to) {
            super(context, datas, resource, from, to);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);

            // 监听删除按钮
            TextView deleteBtn = view.findViewById(R.id.tvDelete);
            deleteBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String path = ObjectUtils.toString(datas.remove(position).get("path"));
                    collectPathDao.deleteByPath(path);
                    adapter.notifyDataSetChanged();

                    // 通知导入页面刷新收藏图标
                    collectPathListListener.refresh();
                }
            });
            return view;
        }

        @Override
        public Map<String, Object> getItem(int position) {
            return datas.get(position);
        }
    }
}
